/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.car.web.front;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.car.entity.GpsCarInfo;
import com.thinkgem.jeesite.modules.car.entity.GpsCompany;
import com.thinkgem.jeesite.modules.car.entity.GpsInfo;
import com.thinkgem.jeesite.modules.car.service.GpsCarInfoService;
import com.thinkgem.jeesite.modules.car.service.GpsCompanyService;
import com.thinkgem.jeesite.modules.car.service.GpsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息Controller
 * @author xiaoqian
 * @version 2018-01-06
 */
@Controller
@RequestMapping(value = "${frontPath}/gps")
public class GpsFrontController extends BaseController {

	@Autowired
	private GpsCarInfoService gpsCarInfoService;
	@Autowired
	private GpsCompanyService gpsCompanyService;
	@Autowired
	private GpsInfoService gpsInfoService;

	/**
	 * 公司列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "companyList")
	public String companyList(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<GpsCompany> list = gpsCompanyService.findList(new GpsCompany());
        model.addAttribute("list", list);
		return "mobile/modules/gps/companyList";
	}

	/**
	 * 获取车辆列表
	 * @param gpsCarInfo
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "gpsCarInfoList")
	public String gpsCarInfoList(GpsCarInfo gpsCarInfo, HttpServletResponse response, Model model) {
		List<GpsCarInfo> list = gpsCarInfoService.findList(gpsCarInfo);
		model.addAttribute("list", list);
		return "mobile/modules/gps/gpsCarInfoList";
	}

	/**
	 * tomap2
	 * @param gpsCarInfo
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "tomap2")
	public String tomap2(GpsCarInfo gpsCarInfo, HttpServletResponse response, Model model) {
		GpsCarInfo carInfo = gpsCarInfoService.get(gpsCarInfo);
		model.addAttribute("carInfo", carInfo);
		return "mobile/modules/gps/tomap2";
	}

	/**
	 * map2
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "map2")
	public String map2(GpsCarInfo gpsCarInfo, HttpServletResponse response, Model model) {
		Date sendTime = gpsCarInfo.getSendTime();
		GpsCarInfo carInfo = gpsCarInfoService.get(gpsCarInfo);
		GpsInfo gpsInfo = new GpsInfo();
		gpsInfo.setSimNo(carInfo.getSimNo());
		gpsInfo.setSendTime(sendTime);
		List<GpsInfo> gpsInfoList = gpsInfoService.findAllList(gpsInfo);
		List<String[]> local = Lists.newArrayList();
		for (GpsInfo info : gpsInfoList) {
			String[] arr = {info.getLongitude(),info.getLatitude()};
			local.add(arr);
		}
		model.addAttribute("gpsInfoList", gpsInfoList);
		model.addAttribute("gpsInfoListStr", JsonMapper.toJsonString(gpsInfoList));
		model.addAttribute("carInfo", carInfo);
		model.addAttribute("str", JsonMapper.toJsonString(local));
		return "mobile/modules/gps/map2";
	}

	/**
	 * 获取实时地图
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "map")
	public String map(GpsCarInfo gpsCarInfo, Model model) {
		GpsCarInfo carInfo = gpsCarInfoService.get(gpsCarInfo);
		GpsInfo gpsInfo = gpsInfoService.findBySim(carInfo.getSimNo());
		model.addAttribute("carInfo", carInfo);
		model.addAttribute("gpsInfo", gpsInfo);
		String img = "";
		if(Objects.equal("1",carInfo.getState())){
			if (Objects.equal("0.0",gpsInfo.getSpeed())){
				img = "http://39.104.51.109/gps-car/static/images/car_red.png";
			}else{
				img = "http://39.104.51.109/gps-car/static/images/car_grey.png";
			}
		}else{
			img = "http://39.104.51.109/gps-car/static/images/car_green.png";
		}
		model.addAttribute("img",img);
		return "mobile/modules/gps/map";
	}

	@ResponseBody
	@RequestMapping(value = "ajaxMap")
	public Object ajaxMap(GpsCarInfo gpsCarInfo, Model model) {
		GpsCarInfo carInfo = gpsCarInfoService.get(gpsCarInfo);
		GpsInfo gpsInfo = gpsInfoService.findBySim(carInfo.getSimNo());
		Map<String,Object> result = Maps.newHashMap();
		result.put("code","200");
		result.put("msg","操作成功");
		result.put("carInfo",carInfo);
		gpsInfo.setSimNo(StringUtils.getSim(gpsInfo.getSimNo()));
		result.put("gpsInfo",gpsInfo);
		return result;
	}


}